# Contributor: Timo Teräs <timo.teras@iki.fi>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

pkgname=edk2
pkgver=0.0.202205
_realver=edk2-stable${pkgver##*.}
_sslver=1.1.1p
_sfver=3e
pkgrel=0
pkgdesc="EFI Development Kit II"
url="https://github.com/tianocore/tianocore.github.io/wiki/EDK-II/"
arch="x86_64 aarch64"
license="BSD-2-Clause-Patent"
makedepends="bash python3 iasl nasm util-linux-dev util-linux-misc"
options="!check" # has no checks
subpackages=""
source="$pkgname-$pkgver.tar.gz::https://github.com/tianocore/edk2/archive/$_realver.tar.gz
	https://www.openssl.org/source/openssl-$_sslver.tar.gz
	http://www.jhauser.us/arithmetic/SoftFloat-$_sfver.zip
	build-hack.patch
	disable-werror.patch
	0008-BaseTools-do-not-build-BrotliCompress-RH-only.patch
	0009-MdeModulePkg-remove-package-private-Brotli-include-p.patch
	"
builddir="$srcdir/$pkgname-$_realver"

case "$CARCH" in
	x86)
		TARGET_ARCH=IA32
		PLATFORM=OvmfPkg/OvmfPkgIa32X64.dsc
		;;
	x86_64)
		TARGET_ARCH=X64
		PLATFORM=OvmfPkg/OvmfPkgX64.dsc
		subpackages="$subpackages ovmf::noarch"
		;;
	aarch64)
		TARGET_ARCH=AARCH64
		PLATFORM=ArmVirtPkg/ArmVirtQemu.dsc
		subpackages="$subpackages aavmf::noarch"
		;;
esac

TOOLCHAIN=GCC5
RELEASE=RELEASE

prepare() {
	# unix line endings for the files to be patched
	sed -e 's/\r$//' -i BaseTools/Source/C/VfrCompile/VfrUtilityLib.cpp \
		BaseTools/Source/C/VolInfo/VolInfo.c
	rm -rf CryptoPkg/Library/OpensslLib/openssl
	ln -s "$srcdir"/openssl-$_sslver CryptoPkg/Library/OpensslLib/openssl
	rm -rf ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3
	ln -s "$srcdir"/SoftFloat-$_sfver \
		ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3
	default_prepare
}

build() {
	export PYTHON_COMMAND=python3
	export WORKSPACE=$PWD
	export PACKAGES_PATH=$PWD
	export EDK_TOOLS_PATH=$PWD/BaseTools/
	export PATH=$PWD/BaseTools/BinWrappers/PosixLike/:$PATH
	# parallel build fails
	unset MAKEFLAGS

	bash -c ". edksetup.sh"
	make -C BaseTools

	msg "Building Plaform Files"
	command build -b $RELEASE \
		-a $TARGET_ARCH  \
		-t $TOOLCHAIN \
		-p $PLATFORM \
		-n ${JOBS:-2}
}

package() {
	mkdir -p "$pkgdir"/usr/bin \
		"$pkgdir"/usr/share/$pkgname/Conf \
		"$pkgdir"/usr/share/$pkgname/Scripts

	install BaseTools/Source/C/bin/* BaseTools/BinWrappers/PosixLike/LzmaF86Compress \
		"$pkgdir"/usr/bin
	install BaseTools/BuildEnv "$pkgdir"/usr/share/$pkgname/
	install BaseTools/Conf/*.template "$pkgdir"/usr/share/$pkgname/Conf
	install BaseTools/Scripts/GccBase.lds "$pkgdir"/usr/share/$pkgname/Scripts

	for i in $(find BaseTools/Source/Python -type d -maxdepth 1); do
		local mod=${i##*/}
		test -f "$i/$mod.py" || continue
		cp -R BaseTools/Source/Python/"$mod" "$pkgdir"/usr/share/edk2/Python/
		cat <<- EOF > "$pkgdir"/usr/bin/"$mod".py
		#!/bin/sh
		export PYTHONPATH=/usr/share/edk2/Python
		exec $PYTHON_COMMAND /usr/share/edk2/Python/$mod/$mod.py "\$@"
		EOF
		chmod +x "$pkgdir"/usr/bin/"$mod".py
	done
}

ovmf() {
	pkgdesc="Open Virtual Machine Firmware (OVMF) BIOS"
	license="BSD MIT"

	for fw in "$builddir"/Build/*/"$RELEASE"_"$TOOLCHAIN"/FV/*.fd; do
		install -D $fw "$subpkgdir"/usr/share/OVMF/${fw##*/}
	done

	# dont ship memfd for now to save space
	rm -f "$subpkgdir"/usr/share/OVMF/MEMFD.fd

	install -d "$subpkgdir"/usr/share/ovmf
	ln -sf ../OVMF/OVMF.fd "$subpkgdir"/usr/share/ovmf/bios.bin
}

aavmf() {
	pkgdesc="ARM (aarch64) Virtual Machine Firmware EFI"
	license="BSD MIT"

	dd if=/dev/zero \
		of="$builddir"/Build/ArmVirtQemu-AARCH64/"$RELEASE"_$TOOLCHAIN/FV/AAVMF_CODE.fd \
		bs=1M seek=64 count=0
	dd if="$builddir"/Build/ArmVirtQemu-AARCH64/"$RELEASE"_$TOOLCHAIN/FV/QEMU_EFI.fd \
		of="$builddir"/Build/ArmVirtQemu-AARCH64/"$RELEASE"_$TOOLCHAIN/FV/AAVMF_CODE.fd \
		conv=notrunc
	dd if=/dev/zero \
	of="$builddir"/Build/ArmVirtQemu-AARCH64/"$RELEASE"_$TOOLCHAIN/FV/AAVMF_VARS.fd \
		bs=1M seek=64 count=0

	for fw in "$builddir"/Build/*/"$RELEASE"_"$TOOLCHAIN"/FV/*.fd; do
		install -D $fw "$subpkgdir"/usr/share/AAVMF/${fw##*/}
	done
}

sha512sums="
0a0d0a8ad7cf23d77d9a6590ccf15e165224c1172e702e61efbf5056b7b62a78ac6dd3bcf2494871c852ee0e69d720db049fdd27e379c21cae64351ec9944428  edk2-0.0.202205.tar.gz
203470b1cd37bdbfabfec5ef37fc97c991d9943f070c988316f6396b09dae7cea16ac884bd8646dbf7dd1ed40ebde6bdfa5700beee2d714d07c97cc70b4e48d9  openssl-1.1.1p.tar.gz
3fedcd0060affb2d8fc7995894133cfed6a495c8717df0d30c89885223c38749f25743598383736036332dad6353c6a3f027f5a94a696660f7c4b607e33e534c  SoftFloat-3e.zip
a7d4ab2c82b62ba01c86e59f53bd3896d661c9bfbb9db9598734155b66d5fe03eca4a2a9993a14d3bf555992c6d01ba5d7a15868ff9ec6ed98b8a9b3895bb7df  build-hack.patch
a7c566f2347ee08ee062be45c340f38dc047c31b1541b8eca994692c8d04f48fea45830dce5933699e9bf9207deeaf22e09639c605a896171f01d9bcd68878ce  disable-werror.patch
ecbfc1ec3b732580c33c477191b71553247af1a68f1754bd363d179e0f5aabde93e3c5ec7f2574f9a9ffefef34e75787a2a87b1057b02cd206e8f0618a252871  0008-BaseTools-do-not-build-BrotliCompress-RH-only.patch
ecad98ff84ab307bda751c8a9a321e064ef880dc66b4d107e66aedbc4e14d00eed76770437e25fa9153dc30803f5cbbf1299329f56865a3b75d2c19f6615e68b  0009-MdeModulePkg-remove-package-private-Brotli-include-p.patch
"
